package com.web;

import com.dao.Impl.goodDAOImpl;
import com.entity.goods;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.ArrayList;
/**
 * @author 20x
 * @date 2024 - 11 -20
 **/
@WebServlet("/delAllGood")
public class DelAllGoodServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String ids = req.getParameter("ids");
        String[] idArray = ids.split(",");
        ArrayList<String> idList = new ArrayList<>();
        for(String id : idArray){
            idList.add(id);
        }
        int count = new goodDAOImpl().delAllGood(idList);
        System.out.println(ids);
        System.out.println(count);
        resp.setContentType("application/json");
        PrintWriter out = resp.getWriter();
        Gson gson = new Gson();
        if(count > 0){
            List<goods> list = new goodDAOImpl().ListGoods();
            out.write(gson.toJson(list));
        }else{
            out.write(gson.toJson("{\"message\":\"数据删除失败\"}"));
        }
    }
}
